package com.cc.controller;


import com.cc.model.EbOrder;
import com.cc.service.OrderService;
import com.cc.vo.OrderProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page" ,defaultValue = "1") int page,
                             @RequestParam(value = "pageSize" ,defaultValue = "3") int pageSize) {
        List<EbOrder> order= orderService.list(page,pageSize);

        int maxPage = orderService.getMaxPage(pageSize);


        ModelAndView mv = new ModelAndView();

        mv.addObject("order",order);
        //绑定当前页
        mv.addObject("curPage",page);
        //b绑定最大页
        mv.addObject("maxPage",maxPage);
        //绑定视图
        mv.setViewName("manage/order");
        return mv;

    }

    @RequestMapping("/save")
    public String save(EbOrder ebOrder) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        String timeStr = sdf.format(date);
        ebOrder.setEo_create_time(timeStr);
        int result = orderService.save(ebOrder);
        if(result<=0){
        }
        return "index";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        int result = orderService.deleteByPk(id);
        System.out.println(result);
        if(result==0){

        }
        return "manage/manage-result1";
    }
    @RequestMapping("/edit")
    public String edit(Model model, Integer eoId){
        EbOrder ebOrders = orderService.selectByCondition(eoId);
        model.addAttribute("ebOrders",ebOrders);
        return "manage/order-modify";
    }
    @RequestMapping("/edit-result")
    public String editResult(EbOrder ebOrder) {
        int result = orderService.updateById(ebOrder);
        System.out.println(result);
        if(result<=0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/select")
    public ModelAndView list1(String eo_user_id,String eo_user_name, @RequestParam(value = "page" ,defaultValue = "1") int page,
                              @RequestParam(value = "pageSize" ,defaultValue = "3") int pageSize) {

        System.out.println("orderes====" +eo_user_id  );
        List<EbOrder> orderss = orderService.listes(eo_user_id,eo_user_name,page,pageSize);
        System.out.println("orders====++++" +orderss  );
        int maxPage = orderService.getMaxPages(eo_user_id,eo_user_name,pageSize);


        ModelAndView mv = new ModelAndView();

        mv.addObject("orders",orderss);
        //绑定当前页
        mv.addObject("curPage",page);
        //b绑定最大页
        mv.addObject("maxPage",maxPage);
        //绑定视图
        mv.setViewName("manage/order1");
        return mv;

    }
    @RequestMapping("/create")
    public String create(Model model , OrderProductVo orderProductVo){
        //下单购买


        EbOrder ebOrder = orderService.createOrder(orderProductVo);
        model.addAttribute("ebOder", ebOrder);

        return "alipay/alipay.trade.page.pay";
    }
}
